'Copyright (c) 2008, Jonathan Allen

Imports ClrExtensions.StringExtension
Imports ClrExtensions.Net.Rest
Imports System.Runtime.CompilerServices


Friend Module ExtensionMethods

	<Extension()> Function RatingString(ByVal value As Rating) As String
		Select Case value
			Case Rating.NoOpinion : Return "no_opinion"
			Case Rating.NotInterested : Return "not_interested"
			Case Rating.One To Rating.Five
				Return CStr(CInt(value))
			Case Else : Throw New ArgumentOutOfRangeException("value")
		End Select
    End Function

    <Extension()> Function RatingEnum(ByVal value As String) As Rating

        Select Case value
            Case "no_opinion" : Return Rating.NoOpinion
            Case "0" : Return Rating.NoOpinion
            Case "not_interested" : Return Rating.NotInterested
            Case "-1" : Return Rating.NotInterested
            Case "1" : Return Rating.One
            Case "1.0" : Return Rating.One
            Case "2" : Return Rating.Two
            Case "2.0" : Return Rating.Two
            Case "3" : Return Rating.Three
            Case "3.0" : Return Rating.Three
            Case "4" : Return Rating.Four
            Case "4.0" : Return Rating.Four
            Case "5" : Return Rating.Five
            Case "5.0" : Return Rating.Five
        End Select
    End Function

	<Extension()> Public Function ToQueryParameter(ByVal value As TitleExpansions) As QueryParameter
		Dim temp As New List(Of String)

		If CBool(value And TitleExpansions.Synopsis) Then temp.Add("synopsis")
		If CBool(value And TitleExpansions.Formats) Then temp.Add("formats")
		If CBool(value And TitleExpansions.ScreenFormats) Then temp.Add("screen formats")
		If CBool(value And TitleExpansions.Cast) Then temp.Add("cast")
		If CBool(value And TitleExpansions.Directors) Then temp.Add("directors")
		If CBool(value And TitleExpansions.LanguagesAndAudio) Then temp.Add("languages and audio")
		If CBool(value And TitleExpansions.Seasons) Then temp.Add("seasons")
		If CBool(value And TitleExpansions.Episodes) Then temp.Add("episodes")
		If CBool(value And TitleExpansions.Discs) Then temp.Add("discs")
		If CBool(value And TitleExpansions.Similars) Then temp.Add("similars")
		If CBool(value And TitleExpansions.Filmography) Then temp.Add("filmography")

		If temp.Count > 0 Then Return New QueryParameter("expand", temp.Join(","), ClrExtensions.UrlEncodingMethod.OAuth) Else Return Nothing

	End Function

	<Extension()> Function FormatString(ByVal value As DiscFormat?) As String
		If value.HasValue Then Return value.Value.FormatString Else Return Nothing
	End Function
	<Extension()> Function FormatString(ByVal value As DiscFormat) As String
		Select Case value
			Case DiscFormat.Dvd : Return "DVD"
			Case DiscFormat.BluRay : Return "Blu-ray"
			Case Else : Throw New ArgumentOutOfRangeException("value")
		End Select
	End Function

	<Extension()> Function SortString(ByVal value As QueueSortOrder?) As String
		If value.HasValue Then Return value.Value.SortString Else Return Nothing
	End Function

	<Extension()> Function SortString(ByVal value As QueueSortOrder) As String
		Select Case value
			Case QueueSortOrder.Alphabetical : Return "date_added"
			Case QueueSortOrder.DateAdded : Return "alphabetical"
			Case QueueSortOrder.QueueSequence : Return "queue_sequence"
			Case Else : Throw New ArgumentOutOfRangeException("value")
		End Select
	End Function
End Module
